package it.apiconsulting.sharem.mediaplayer;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class Player {

	public Long idLocale;
	
	public String ipLocale;
	
	public InetAddress host;
	
	public BufferedReader in;
	
	public OutputStream out;
	
	public Process process;
	
	public Socket socket;
	
	public String url;
	
	public String logger;
	
	public boolean pause=false;
	
	public Player (String url, Long idLocale, String ipLocale){
		
		this.ipLocale="localhost";
		this.url=url;
		this.idLocale=idLocale;
		try {
			
			/* CONTROLLO SE VLC PLAYER E' GIA' ATTIVO */
			String line;
			boolean found=false;
			Process p=Runtime.getRuntime().exec(System.getenv("windir") +"\\system32\\"+"tasklist.exe");
			BufferedReader input =  new BufferedReader(new InputStreamReader(p.getInputStream()));
			while ((line = input.readLine()) != null || found) {
			    if ( line.trim().contains("vlc.exe") ) {
			    	found = true;
			    	break;
			    }
			}
			input.close();
			
			if(found) {
				/* VLC PLAYER E' GIA' ATTIVO */
				System.out.println("Processo attivo");
			} else {
				/* VLC PLAYER VA ATTIVATO */
				this.process = Runtime.getRuntime().exec("C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe --extraintf rc --rc-host=" + this.ipLocale + ":8181");
				
				/* INIZIALIZZO IL CONTROLLER */
				this.host = InetAddress.getByName(this.ipLocale);
				this.socket = new Socket(host, 8181);
				this.out = this.socket.getOutputStream();
				this.in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
			}
		} catch( Exception e ){
			e.printStackTrace();
		}
	}
	
	public Player (String url, Long idLocale){
		
		this.url=url;
		this.idLocale=idLocale;
		try {
			
			/* CONTROLLO SE VLC PLAYER E' GIA' ATTIVO */
			String line;
			boolean found=false;
			Process p=Runtime.getRuntime().exec(System.getenv("windir") +"\\system32\\"+"tasklist.exe");
			BufferedReader input =  new BufferedReader(new InputStreamReader(p.getInputStream()));
			while ((line = input.readLine()) != null || found) {
			    if ( line.trim().contains("vlc.exe") ) {
			    	found = true;
			    	break;
			    }
			}
			input.close();
			
			if(found) {
				/* VLC PLAYER E' GIA' ATTIVO */
				System.out.println("Processo attivo");
			} else {
				/* VLC PLAYER VA ATTIVATO */
				this.process = Runtime.getRuntime().exec("C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe --extraintf rc --rc-host=localhost:8181");
				
				/* INIZIALIZZO IL CONTROLLER */
				this.host = InetAddress.getByName("localhost");
				this.socket = new Socket(host, 8181);
				this.out = this.socket.getOutputStream();
				this.in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
			}
		} catch( Exception e ){
			e.printStackTrace();
		}
	}
	
	public void isPlaying(){
		
		try {
			/* INVIO COMANDO */
			out.write("is_playing\n".getBytes());
			
			/* ESITO COMANDO */
			String stato=in.readLine();
			if (stato.equals("1")) System.out.println("OK");
			else System.out.println("KO");
		} catch( Exception e ){
			e.printStackTrace();
		}
	}

	public void play(String path){
		
		try {
			/* INVIO COMANDO */
			if ( !pause ) {
				if ( path != null && !path.equals("") ) {
					String track = "add " + path + "\n";
					out.write(track.getBytes());
				} else out.write("play\n".getBytes());
			} else {
				out.write("pause\n".getBytes());
			}
			
			/* ESITO COMANDO */
			boolean ok = true;
			while (ok) {
				try {
					String readL=in.readLine();
					if (readL.contains("(no error)")){
						ok=false;
					}
					System.out.println(readL);
				} catch (Exception e) {
					System.out.println("Whoops: " + e.getMessage());
					ok = false;
				}
			}
		} catch( Exception e ){
			e.printStackTrace();
		}	
	}
	
	public void enqueue(String path){
		
		try {
			/* INVIO COMANDO */
			String track = "enqueue " + path + "\n";
			out.write(track.getBytes());
			
			/* ESITO COMANDO */
			boolean ok = true;
			while (ok) {
				try {
					String readL=in.readLine();
					if (readL.contains("(no error)")){
						ok=false;
					}
					System.out.println(readL);
				} catch (Exception e) {
					System.out.println("Whoops: " + e.getMessage());
					ok = false;
				}
			}
		} catch( Exception e ){
			e.printStackTrace();
		}
	}
	
	public List<String> playlist(){
		
		List<String> trackList = new ArrayList<String>();
		List<String> list = new ArrayList<String>();
		try {
			/* INVIO COMANDO */
			out.write("playlist\n".getBytes());
			
			/* ESITO COMANDO */
			boolean ok = true;
			boolean read = false;
			while (ok) {
				try {
					String readL=in.readLine();
					if (readL.contains("(no error)")){
						ok=false;
					} else if (readL.contains("|- Raccolta multimediale")){
						read=false;
					} else if (readL.contains("|- Scaletta")){
						read=true;
					} else if (read) {
						list.add(readL);
					}
					
				} catch (Exception e) {
					System.out.println("Whoops: " + e.getMessage());
					ok = false;
				}
			}
			
			/* PULIZIA LISTA */
			for ( int i = 0; i < list.size(); i++ ) {
				
				trackList.add( list.get(i).replace("|  - ", "") );
			}
		} catch( Exception e ){
			e.printStackTrace();
		}
		return trackList;
	}
	
	public void pause(){
		
		try {
			/* INVIO COMANDO */
			out.write("pause\n".getBytes());
			pause=!pause;
			/* ESITO COMANDO */
			boolean ok = true;
			while (ok) {
				try {
					String readL=in.readLine();
					if (readL.contains("(no error)")){
						ok=false;
					}
					System.out.println(readL);
				} catch (Exception e) {
					System.out.println("Whoops: " + e.getMessage());
					ok = false;
				}
			}
		} catch( Exception e ){
			e.printStackTrace();
		}	
	}
	
	public void stop(){
		
		try {
			/* INVIO COMANDO */
			out.write("stop\n".getBytes());
			
			/* ESITO COMANDO */
			boolean ok = true;
			while (ok) {
				try {
					String readL=in.readLine();
					if (readL.contains("(no error)")){
						ok=false;
					}
					System.out.println(readL);
				} catch (Exception e) {
					System.out.println("Whoops: " + e.getMessage());
					ok = false;
				}
			}
		} catch( Exception e ){
			e.printStackTrace();
		}
	}
	
	public void next(){
		
		try {
			/* INVIO COMANDO */
			out.write("next\n".getBytes());
			
			/* ESITO COMANDO */
			boolean ok = true;
			while (ok) {
				try {
					String readL=in.readLine();
					if (readL.contains("(no error)")){
						ok=false;
					}
					System.out.println(readL);
				} catch (Exception e) {
					System.out.println("Whoops: " + e.getMessage());
					ok = false;
				}
			}
		} catch( Exception e ){
			e.printStackTrace();
		}
	}
	
	public void prev(){
		
		try {
			/* INVIO COMANDO */
			out.write("prev\n".getBytes());
			
			/* ESITO COMANDO */
			boolean ok = true;
			while (ok) {
				try {
					String readL=in.readLine();
					if (readL.contains("(no error)")){
						ok=false;
					}
					System.out.println(readL);
				} catch (Exception e) {
					System.out.println("Whoops: " + e.getMessage());
					ok = false;
				}
			}
		} catch( Exception e ){
			e.printStackTrace();
		}
	}
	
	public void quit(){
		
		try {
			out.write("clear\n".getBytes());
			out.write("quit\n".getBytes());
			boolean ok = true;
			while (ok) {
				try {
					String readL=in.readLine();
					if (readL.contains("(no error)")){
						ok=false;
					}
					System.out.println(readL);
				} catch (Exception e) {
					System.out.println("Whoops: " + e.getMessage());
					ok = false;
				}
			}
		} catch( Exception e ){
			e.printStackTrace();
		}
	}

	public BufferedReader getIn() {
		return in;
	}

	public void setIn(BufferedReader in) {
		this.in = in;
	}

	public OutputStream getOut() {
		return out;
	}

	public void setOut(OutputStream out) {
		this.out = out;
	}

	public Process getProcess() {
		return process;
	}

	public void setProcess(Process process) {
		this.process = process;
	}

	public Socket getSocket() {
		return socket;
	}

	public void setSocket(Socket socket) {
		this.socket = socket;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
}
